package com.basic.thread.model;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * AtomicInteger Realization
 * <p>
 * 
 *
 * @author <a href="mailto:lysongfei@gmail.com">songfei</a>
 * @version 1.0, 2013-4-8
 */
public class AtomicBankAccount {
	
	private AtomicInteger money;
	
	public AtomicBankAccount(int money) {
		this.money.set(money);
	}

	public int getMoney() {
		return money.get();
	}


	public void deposit(int amount) {
		money.addAndGet(amount);
	}
	
	public void withdraw(int amount) {
		int val = money.get();
		if(val >= amount){
			if(money.compareAndSet(val, val-amount)){
				
			}else{
				
			}
		}else{
			throw new RuntimeException("Account is not enough ... ");
		}
	}
	
	
}
